<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {
    public function rand_str($len)
    {
        $content = "";
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
        for ($i = 0; $i < $len; $i++) {
            $content .= $pattern{mt_rand(0, 35)};

        }
        return $content;
    }

    public function index(){
        if(IS_POST){
            $json_data = php_input();
            if(!is_null(json_decode($json_data))){
                //json数据，IOS请求数据
                $input = json_decode($json_data,true);
            }else{
                //非json数据，安卓请求数据
                parse_str($json_data, $input);
            }

            $uid = $input['uid']?$input['uid']:0;

            $skill_id = $input['skill_id'];
            $n = $input['n']?$input['n']:0;
            $num = $input['num']?$input['num']:10;

            $type = $input['type']?$input['type']:'';

            $u_lng = $input['u_lng']?$input['u_lng']:0;//用户经度
            $u_lat = $input['u_lat']?$input['u_lat']:0;//用户纬度

            /***************直播列表*****************/

            $rooms = rooms_data('index');

            if($rooms){
                foreach($rooms as $k => $v){
                    $number = $v['roomCreator'];
                    $info = M('user')->field('id as auid,skill_id,anchor_time,funs_num,cover,nickname,lng,lat')->where(['number' => $number])->find();

                    //计算距离
                    $rooms[$k]['distance'] = getDistance_map($info['lng'],$info['lat'],$u_lng,$u_lat);

                    if($type == 'sys'){
                        //系统分类
                        if($info['skill_id'] != $skill_id){
                            unset($rooms[$k]);
                        }
                    }
                }

                if($type == 'new'){
                    //最新
                    $rooms = my_sort($rooms, 'id', SORT_DESC);
                }elseif($type == 'hot'){
                    //最热
                    $rooms = my_sort($rooms, 'funs_num', SORT_DESC);
                }

                //直播列表分页
                $rooms = page_array($num,$n+1,$rooms);
            }
            /***************直播列表*****************/

            $list = M('advert')->select();//banner列表

            foreach($list as $k => $v){
                $list[$k]['img'] = get_img($v['img']);
                if(empty($v['link_url'])){
                    //内链
                    $list[$k]['live'] = advert_room($v['link_id']);
                }else{
                    //外链
                    $list[$k]['live'] = advert_room();
                }
            }

            $navs = M('skill')->select();//顶部导航

            if($uid){
                $sys_mess = mess_count($uid);//未读系统消息
                $concern_mess = user_concern_mess_count($uid);//未读关注消息
                $count = $sys_mess+$concern_mess;
            }else{
                $count = 0;
            }

            $data = [
                'users' => $rooms?$rooms:[],
                'list' => $list?$list:[],
                'navs' => $navs?$navs:[],
                'count' => $count
            ];

            if($rooms){
                return api_return(1,'获取直播数据成功',$data);
            }else{
                return api_return(1,'无直播数据',$data);
            }
        }
    }

    public function kdtest(){
        $num = $_REQUEST['tsn'];
        $com = 'yunda';
        $post_data = array();
        $post_data["customer"] = '94B5EB89CAF0B36265FF974EA61985F3';
        $key= 'CdrfmsSw5290' ;
        $param = array('com'=>$com,'num'=>$num);
        $post_data["param"] = json_encode($param);
       $url='https://poll.kuaidi100.com/poll/query.do';
     //   $url="https://m.kuaidi100.com/index_all.html?type=中通快递&postid=212518939714";

        $post_data["sign"] = md5($post_data["param"].$key.$post_data["customer"]);
        $post_data["sign"] = strtoupper($post_data["sign"]);
        $o="";
        foreach ($post_data as $k=>$v)
        {
            $o.= "$k=".urlencode($v)."&";
        }
        $post_data=substr($o,0,-1);
        $data=curl($url,$post_data);
      print_r($data);
    }
}